//
//  MCUserDefinedLotteryCategoriesViewController.m
//  TLYL
//
//  Created by MC on 2017/6/19.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCUserDefinedLotteryCategoriesViewController.h"
#import "MCUserDefinedLotteryCategoriesTableViewCell.h"
#import "MCUserDefinedLotteryCategoriesHeaderView.h"
#import "MCMineCellModel.h"
#import "MCBasePWFModel.h"
#import "MCPullMenuModel.h"
#import "MCDataTool.h"
@interface MCUserDefinedLotteryCategoriesViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray*marr_Section;
@property(nonatomic, strong)NSDictionary * dic_CZHelper;


/*
 * 设置选中彩种
 */
@property(nonatomic, strong)NSMutableArray<MCUserDefinedLotteryCategoriesModel*>*marr_LotteryCategories;


@end

@implementation MCUserDefinedLotteryCategoriesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setProperty];
    
    [self setNav];
    
    [self createUI];
    
    [self loadData];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

#pragma mark==================setProperty======================
-(void)setProperty{
    
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationItem.title = @"自定义彩种";
    _marr_Section=[[NSMutableArray alloc]init];
    _marr_LotteryCategories=[[NSMutableArray alloc]init];
    [_marr_LotteryCategories addObjectsFromArray:[MCDataTool MC_GetMarr_withID:MCHomePageLotteryCategoryData]];
    
}

#pragma mark ====================设置导航栏========================
-(void)setNav{
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 28, 28);
    [rightBtn addTarget:self action:@selector(action_rightBtn) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setImage:[UIImage imageNamed:@"MCUserDefined_Sure"] forState:UIControlStateNormal];
    UIBarButtonItem *rewardItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -7;
    self.navigationItem.rightBarButtonItems = @[spaceItem,rewardItem];
    
}
#pragma mark ======点击设置完成
-(void)action_rightBtn{
    
    /*
     * 存储数据
     */
    [MCDataTool MC_SaveMarr:_marr_LotteryCategories withID: MCHomePageLotteryCategoryData];
    
    if (self.block) {
        self.block(_marr_LotteryCategories);
    }
    [self.navigationController  popViewControllerAnimated:YES];
    
}

#pragma mark==================createUI======================
-(void)createUI{
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.and.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
    }];
}
/*
 * 数据
 */
-(NSMutableArray * )GetData{
    //时时彩
    NSArray * arr_ssc=@[@"12",@"66",@"75",@"56",@"71",@"14",@"86",@"73"];
    
    //竞速彩
    NSArray * arr_jsc=@[@"50",@"51",@"53",@"55"];
    
    //快乐彩
    NSArray * arr_klc=@[@"26",@"82",@"83",@"15",@"10",@"9",@"79",@"80"];
    
    //低频彩
    NSArray * arr_dpc=@[@"19",@"84",@"17",@"18"];
    
    //11选5
    NSArray * arr_esf=@[@"61",@"63",@"85",@"4",@"78",@"16",@"5",@"77"];
    
    NSArray * arr_CZ=@[arr_ssc,arr_jsc,arr_klc,arr_dpc,arr_esf];
    
    NSMutableArray * marr_CZ=[[NSMutableArray alloc]init];
    for (NSArray * item in arr_CZ) {
        NSMutableArray * marr_item=[[NSMutableArray alloc]init];
        for (NSString * type in item) {
            MCUserDefinedLotteryCategoriesModel * model=[MCUserDefinedLotteryCategoriesModel mj_objectWithKeyValues:[self.dic_CZHelper objectForKey:type]];
            model.categoryID=type;
            model.isSelected=[self Get_isSelectedWithID:type];
            [marr_item addObject:model];
        }
        [marr_CZ addObject:marr_item];
    }
    return marr_CZ;
}

-(NSInteger)Get_isSelectedWithID:(NSString *)categoryID{
    /*
     * 比对哪些是被选中的  【选中就放上选中标记】
     */
    for (MCUserDefinedLotteryCategoriesModel * item in _marr_LotteryCategories) {
        if ([item.categoryID isEqualToString:categoryID]) {
            return 1;
        }
    }
    return 0;
}

#pragma mark==================loadData======================
-(void)loadData{
    
    
    [self setMarr_Section];
    
    [_tableView reloadData];
    
}

-(void)setMarr_Section{
    
    NSMutableArray * marr=[self GetData];
    NSArray * arr_Name=@[@"时时彩",@"竞速彩",@"快乐彩",@"低频彩",@"11选5"];
    [_marr_Section removeAllObjects];
    int i=0;
    for (NSArray * item1 in marr) {
        NSMutableArray * marr_Model=[[NSMutableArray alloc]init];
        for(int j=0 ;j<item1.count; j++){
            CellModel* model =[[CellModel alloc]init];
            model.reuseIdentifier = NSStringFromClass([MCUserDefinedLotteryCategoriesTableViewCell class]);
            model.className=NSStringFromClass([MCUserDefinedLotteryCategoriesTableViewCell class]);
            model.height = [MCUserDefinedLotteryCategoriesTableViewCell computeHeight:nil];
            model.selectionStyle=UITableViewCellSelectionStyleNone;
            model.accessoryType=UITableViewCellAccessoryNone;
            /*
             * 传递参数
             */
            model.userInfo=item1[j];
            [marr_Model addObject:model];
            
        }
        SectionModel *model0=[SectionModel sectionModelWithTitle:@"" cells:marr_Model];
        model0.headerhHeight=[MCUserDefinedLotteryCategoriesHeaderView computeHeight:nil];
        model0.footerHeight=0.00001;
        model0.userInfo=arr_Name[i];
        model0.className=NSStringFromClass([MCUserDefinedLotteryCategoriesHeaderView class]);
        [_marr_Section addObject:model0];
        i++;
    }
}
#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _marr_Section.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.cells.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.headerhHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.footerHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    MCUserDefinedLotteryCategoriesHeaderView * view=[[MCUserDefinedLotteryCategoriesHeaderView alloc]init];
    view.dataSource=sm.userInfo;
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    return cm.height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    
    if ([cm.className isEqualToString:NSStringFromClass([MCUserDefinedLotteryCategoriesTableViewCell class])]) {
        
        MCUserDefinedLotteryCategoriesTableViewCell *ex_cell=(MCUserDefinedLotteryCategoriesTableViewCell *)cell;
        ex_cell.dataSource=cm.userInfo;
        
        //        __block MCUserDefinedLotteryCategoriesViewController *weakSelf=self;
        //        __block MCUserDefinedLotteryCategoriesTableViewCell* weak_cell=ex_cell;
        //        ex_cell.block=^(MCUserDefinedLotteryCategoriesModel *model){
        //            if (model.isSelected) {
        //                if (weakSelf.marr_LotteryCategories.count>7) {
        //                    /*
        //                     * 最多只能选择8个彩种
        //                     */
        //                    [MBProgressHUD showError:@"最多只能选择8个彩种" toView:self.view];
        //
        //                    [weak_cell.btn_Selected setSelected:NO];
        //
        //                    return;
        //                }else{
        //                    [weakSelf addModel:model];
        //
        //                }
        //
        //            }else{
        //                [weakSelf removeModel:model];
        //            }
        //        };
    }
    return cell;
}

#pragma mark-addModel
-(void)addModel:(MCUserDefinedLotteryCategoriesModel * )model{
    
    [self.marr_LotteryCategories addObject:model];
    
    [self setMarr_Section];
    
}

#pragma mark-removeModel
-(void)removeModel:(MCUserDefinedLotteryCategoriesModel * )model{
    
    for (MCUserDefinedLotteryCategoriesModel * item in _marr_LotteryCategories) {
        if ([item.categoryID isEqualToString:model.categoryID]) {
            [_marr_LotteryCategories removeObject:item];
            return;
        }
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    MCUserDefinedLotteryCategoriesTableViewCell* cell=[_tableView cellForRowAtIndexPath:indexPath];
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    MCUserDefinedLotteryCategoriesModel * model=cm.userInfo;
    
    if (!cell.btn_Selected.selected) {
        if (self.marr_LotteryCategories.count>7) {
            /*
             * 最多只能选择8个彩种
             */
            [MBProgressHUD showError:@"最多只能选择8个彩种" toView:self.view];
            
            return;
        }else{
            
            model.isSelected=1;
            [self addModel:model];
            [cell.btn_Selected setSelected:YES];
            
        }
        
    }else{
        model.isSelected=0;
        [self removeModel:model];
        [cell.btn_Selected setSelected:NO];
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark-懒加载
-(NSDictionary *)dic_CZHelper{
    if (!_dic_CZHelper) {
        _dic_CZHelper = [MCDataTool MC_GetDic_CZHelper];
    }
    return _dic_CZHelper;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end









